﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpGen
{
    /// <summary>
    /// Auflistung mit allen Textteilen einer Zeile
    /// </summary>
    public class LineSplitter : IEnumerable<string>
    {
        private List<string> _List = new List<string>();
        
        /// <summary>
        /// Erstellt eine neue Instanz der LineParts-Klasse
        /// </summary>
        /// <param name="Line"></param>
        public LineSplitter(string Line)
        {
            IsParameter = Line.StartsWith("@");

            // @ Entfernen
            if (IsParameter)
                Line = Line.Substring(1);

            ParseLine(Line);
        }

        private void ParseLine(string Line)
        {
            var Inline = false;
            var Word = "";

            foreach (var item in Line)
            {
                // Wortende?
                if (item == ' ' && Inline == false)
                {
                    if (!string.IsNullOrEmpty(Word)) _List.Add(Word);
                    Word = "";
                    continue;
                }

                if (item == '(')
                {
                    Inline = true;
                }

                if (item == ')')
                {
                    Word += item;
                    if (!string.IsNullOrEmpty(Word)) _List.Add(Word);
                    Word = "";
                    continue;
                }

                Word += item;
            }

            if (!string.IsNullOrEmpty(Word)) _List.Add(Word);
        }

        /// <summary>
        /// Liefert die Anzahl der Wörter
        /// </summary>
        public int Count
        {
            get { return _List.Count; }
        }

        /// <summary>
        /// Liefert ein Wort anhand des Index
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public string this[int index]
        {
            get { return _List[index]; }
            set { _List[index] = value; }
        }

        /// <summary>
        /// Entfernt ein Wort anhand des Index
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            _List.RemoveAt(index);
        }

        /// <summary>
        /// Gibt an ob ein Wort in der Liste verfügbar ist
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public bool Contains(string Text)
        {
            return ContainsCount(Text.Trim()) > 0;
        }

        /// <summary>
        /// Liefert die Anzahl des Wortes
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public int ContainsCount(string Text)
        {
            return (from Item in _List where Item.ToLower() == Text.ToLower() select Item).Count();
        }

        /// <summary>
        /// Liefert einen Wort-Teil das den Text enthält
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public string ContainsPart(string Text)
        {
            var Ret = (from Item in _List where Item.ToLower() == Text.ToLower() select Item);

            if (Ret.Count() == 0)
                return "";

            return Ret.First();
        }

        /// <summary>
        /// Entfernt ein Wort
        /// </summary>
        /// <param name="Text"></param>
        public void Remove(string Text)
        {
            _List = (from Item in _List where !(Item.ToLower() == Text.ToLower()) select Item).ToList();
        }

        /// <summary>
        /// Gibt an ob die Zeile ein Parameter ist
        /// </summary>
        public bool IsParameter { get; private set; }

        public IEnumerator<string> GetEnumerator()
        {
            return _List.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _List.GetEnumerator();
        }
    }
}
